rm(list=ls(all=TRUE))
library(ggplot2)
library(gridExtra)
require(fBasics)
library(arm)
library(stringr)
library(car)
library(abind)
library(readstata13)
library(reshape2)
library(plyr)
require(data.table)

setwd("C:/Users/adamz/Dropbox/RSF Ineqaulity Project/Replication File")
setwd()


ggtheme_personalized <- theme(panel.background = element_blank(),
		panel.grid = element_blank(),
		axis.ticks = element_blank(),
		axis.title.x = element_text(color = "black" , size = 12, face="bold"),
		axis.title.y = element_text(color = "black" , size = 12, face="bold"),
		axis.text.y = element_text(size = 8, colour = "black"),
		axis.text.x = element_text(size = 8, colour = "black"),
		panel.border = element_blank(),
		legend.key = element_rect(colour = "white",fill = "white"),
		legend.text = element_text(size=10,color="black"),
		legend.title = element_text(size = 11, face = "bold", colour = "black"),
		strip.text.x = element_text(size = 11, face = "bold", colour = "black"),
		strip.background = element_rect(colour = "white", fill = "white"),
		legend.position="none",
		plot.margin = unit(c(0.5,0.5,0.5,0.5), "cm"))


###########################################################################
#################     Create DF of opinion and votes     ##################
###########################################################################

opinion <- read.csv(file = "Formatted Data/opinion_estimates_with_nationalQ1-Q5.csv" , stringsAsFactors=FALSE)
opinion <- subset(opinion , issue!="Partial Birth Abortion (2006)")
votes <- read.csv(file = "Formatted Data/roll_call_votes_final.csv" , stringsAsFactors=FALSE)

df <- merge(opinion , votes , by = c("state","issue"))
df <- subset(df , vote!=9 & issue!="Partial Birth Abortion (2006)")
### Convert Bernie, Angus, Jeffords, and Lieberman to Dem
df$party[df$party%in%c("I","ID")] <- "D"

### Add category
issues <- read.csv(file = "list_of_issues.csv" , stringsAsFactors=FALSE)
setnames(issues , "bill" , "issue")
issues <- subset(issues , vote==1)
df <- join(df , issues[,c("issue","category")] , by = "issue")
df2 <- unique(df[,c(1:26,35)])


###########################################################################
############     SUMMMARY STATISTICS (Section 4 of paper)     #############
###########################################################################
with(df2 , mean(abs(Q1 - Q5)))
with(df2 , mean(Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ))

ddply(df2 , .(category) , function(x) mean(abs(x$Q1 - x$Q5)))
temp <- ddply(df2 , .(issue,category) , function(x) mean(abs(x$Q1 - x$Q5)))
mean(temp$V1)
temp[order(temp$V1),]

with(df2 , mean(abs(Dem - Rep)))
with(df2 , mean(Dem < 0.5 & Rep > 0.5 | Dem > 0.5 & Rep < 0.5 ))
temp <- ddply(df2 , .(issue,category) , function(x) mean(abs(x$Dem - x$Rep)))
mean(temp$V1)
temp[order(temp$V1),]
temp <- ddply(temp , .(category) , function(x) mean(x$V1))
temp


with(subset(df2 , category=="Social"), mean(Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ))
with(subset(df2 , category=="Economic"), mean(Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ))
with(subset(df2 , category=="Security"), mean(Q1 < 0.5 & Q5 > 0.5 | Q1 > 0.5 & Q5 < 0.5 ))

with(subset(df2 , category=="Social"), mean(Dem < 0.5 & Rep > 0.5 | Dem > 0.5 & Rep < 0.5 ))
with(subset(df2 , category=="Economic"), mean(Dem < 0.5 & Rep > 0.5 | Dem > 0.5 & Rep < 0.5 ))
with(subset(df2 , category=="Security"), mean(Dem < 0.5 & Rep > 0.5 | Dem > 0.5 & Rep < 0.5 ))


## Range of income polarization by state
temp <- ddply(df2 , .(state) , function(x) mean(abs(x$Q1-x$Q5)))
mean(temp[order(temp$V1),]$V1)
sd(temp[order(temp$V1),]$V1)
head(temp[order(temp$V1),])
tail(temp[order(temp$V1),])

## Range of party polarization by state
temp <- ddply(df2 , .(state) , function(x) mean(abs(x$Rep-x$Dem)))
mean(temp[order(temp$V1),]$V1)
sd(temp[order(temp$V1),]$V1)
head(temp[order(temp$V1),])
tail(temp[order(temp$V1),])

# When rich and poor disagree, how frequently do dems and reps align with rich?
ddply(subset(df , Q1 > 0.5 & Q5 < 0.5 | Q1 < 0.5 & Q5 > 0.5), . (party) , function(x)
	mean(x$Q5 > 0.5 & x$vote==1 | x$Q5 < 0.5 & x$vote==0)
)


# When state and Q3 disagree, what are the issues?
dim(subset(df , Q1 < 0.5 & SW > 0.5 | Q1 > 0.5 & SW < 0.5))
dim(subset(df , Q3 < 0.5 & SW > 0.5 | Q3 > 0.5 & SW < 0.5))
dim(subset(df , Q3 < 0.5 & Q1 > 0.5 & SW > 0.5 | Q3 > 0.5 & Q1 < 0.5 & SW < 0.5))


# When state and Q3 disagree, what are the issues?
dim(subset(df , Q1 < 0.5 & SW > 0.5 | Q1 > 0.5 & SW < 0.5))
dim(subset(df , Q3 < 0.5 & SW > 0.5 | Q3 > 0.5 & SW < 0.5))
head(df2)

###########################################################################
###############                    Table 1                 ################
###########################################################################
# what is group by group agreement?
tmp <- subset(df2 , select = c("SW","Q1","Q5","Dem","Rep","Ind","Q1D","Q5D","Q1R","Q5R"))
tmp <- apply(tmp , 2 , function(x) as.numeric(x > 0.5))
head(tmp)

out <- matrix(NA, nrow = 10, ncol = 10)
colnames(out) <- rownames(out) <- colnames(tmp)

for(i in 1:ncol(tmp)){
  for(j in 1:ncol(tmp)){
    out[i,j] <- mean(tmp[,i]==tmp[,j])
  }
}
round(out , 2)
head(tmp)

###########################################################################
###############                   Figures                  ################
###########################################################################

gg_color_hue <- function(n) {
  hues = seq(15, 375, length = n + 1)
  hcl(h = hues, l = 65, c = 100)[1:n]
}

n = 4
cols = gg_color_hue(n)
cols
names(cols) <- c("Security","Economic","Social","Other")



###########################################################################
###############                   Figure 1                 ################
###########################################################################

order.temp <- ddply(df2 , .(issue,category) , function(x) mean(abs(x$Q5 - x$Q1)))
order.temp$category <- factor(order.temp$category , levels= c("Security","Economic","Social"))
order.temp <- order.temp[order(-as.numeric(order.temp$category),order.temp$V1),]
order.temp$xaxis <- gl(nrow(order.temp) , 1 , nrow(order.temp))

ggdata <- join(df2 , order.temp[,c(1,4)])
ggdata <- ggdata[order(ggdata$xaxis),]

pdf("Figures for Paper/Figure_1_polarization_by_party_by_issue2.pdf",height=12,width=10,onefile=FALSE)
ggplot(ggdata, aes(y=abs(Dem - Rep)*100,x=as.numeric(xaxis)-0.,group=issue)) +
  geom_boxplot(coef = 0, outlier.shape=NA, fill="grey50") +
  geom_boxplot(data = ggdata , aes(y=abs(Q5 - Q1)*100,x=as.numeric(xaxis)+0.,group=issue), coef = 0,
               outlier.shape=NA, fill="grey90") +
  scale_fill_discrete(guide = guide_legend(title = "Issue Type",reverse=FALSE))+
  ylab("Partisan (Dark) and Income (Light) Opinion Difference (perc. pts.)")+xlab("")+
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 10.))+
  scale_x_continuous(labels = gsub("_"," ",unique(ggdata$issue)), breaks = 1:49 , limits = c(0,50)) + coord_flip() +
  ggtheme_personalized + theme(legend.position="right") +
  theme(axis.text.x=element_text(size=10)) +
  theme(axis.text.y=element_text(size=10)) +
  theme(legend.title=element_blank()) +
  geom_vline(aes(xintercept = 15.5) , colour = "grey30" , size = .75, linetype = 2) +
  geom_vline(aes(xintercept = 43.5) , colour = "grey30" , size = .75 , linetype = 2) +
  annotate("text", x = 8.5 , y = 65 , label =  "Social Issues", parse = FALSE, colour = "grey50", size = 8) +
  annotate("text", x = 30.5 , y = 65 , label =  "Economic Issues", parse = FALSE, colour = "grey50", size = 8) +
  annotate("text", x = 46.1 , y = 65 , label =  "Security Issues", parse = FALSE, colour = "grey50", size = 8) +
  theme(panel.grid.major.y=element_blank()) +
  ylim(c(0,82))+
  theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


order.temp <- ddply(df2 , .(issue,category) , function(x) out = data.frame(
	partisan = mean(abs(x$Dem - x$Rep)),
	income = mean(abs(x$Q1 - x$Q5))))

order.temp <- ddply(df2 , .(issue,category) , function(x) out = data.frame(
	partisan = mean(x$Dem < 0.5 & x$Rep > 0.5 | x$Dem > 0.5 & x$Rep < 0.5),
	income = mean(x$Q1 < 0.5 & x$Q5 > 0.5 | x$Q1 > 0.5 & x$Q5 < 0.5)))
order.temp$category <- factor(order.temp$category , levels= c("Security","Economic","Social"))
order.temp <- order.temp[order(-as.numeric(order.temp$category),order.temp$income),]
order.temp$xaxis <- gl(nrow(order.temp) , 1 , nrow(order.temp))

pdf("Figures for Paper/Figure_A1_divergence_partisan_and_income_by_issue.pdf",height=12,width=10,onefile=FALSE)
ggplot(order.temp, aes(y=partisan*100,x=xaxis)) +
	geom_point(aes(colour=category, shape = category), alpha = 1 ,size = 4) +
	geom_point(aes(y = income*100, colour=category, shape = category), alpha = 1 ,size = 4) +
	geom_point(aes(y = income*100, shape = category), colour="white", alpha = 1 ,size = 1 , show.legend=FALSE) +
	scale_fill_discrete(guide = guide_legend(title = "Issue Type",reverse=FALSE))+
	ylab("\n% Disagreement between Parties (Solid) and Income Groups (Hollow) across States")+xlab("")+
	theme(axis.title.x=element_blank(),
	axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.25))+
	scale_x_discrete(labels = order.temp$issue) + coord_flip() +
	ggtheme_personalized + theme(legend.position="right") +
	theme(axis.text.x=element_text(size=10)) +
	theme(axis.text.y=element_text(size=10)) +
	theme(legend.title=element_blank()) +
	geom_vline(aes(xintercept = 15.5) , colour = "grey30" , size = .75, linetype = 2) +
	geom_vline(aes(xintercept = 43.5) , colour = "grey30" , size = .75 , linetype = 2) +
	theme(panel.grid.major.y=element_blank()) +
	ylim(c(0,100))+
	theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()

pdf("Figures for Paper/Figure_2b_divergence_partisan_and_income_by_issue_twoway.pdf",height=6,width=6,onefile=FALSE)
ggplot(order.temp, aes(y=income*100,x=partisan*100)) +
	geom_jitter(aes(colour=category, shape = category), alpha = 0.75 ,size = 4, width = 0.02, height = 0.02) +
	scale_fill_discrete(guide = guide_legend(title = "Issue Type",reverse=FALSE))+
	ylab("% Disagreement between Income Groups")+xlab("\n% Disagreement between Parties")+
	theme(axis.title.x=element_blank(),
	axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.25))+
	ggtheme_personalized + theme(legend.position="top") +
	theme(axis.text.x=element_text(size=12)) +
	theme(axis.text.y=element_text(size=12)) +
	theme(legend.title=element_blank()) +
	theme(panel.grid.major.y=element_blank()) +
	ylim(c(-005,105))+ xlim(c(-005,105))+
	geom_segment(x = 0, xend = 100, y = 0, yend = 100, color = "grey70",linetype =3) +
	theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


order.temp <- ddply(df2 , .(issue,category) , function(x) out = data.frame(
  partisan = mean(abs(x$Dem - x$Rep )),
  income = mean(abs(x$Q1 - x$Q5))))
order.temp$category <- factor(order.temp$category , levels= c("Security","Economic","Social"))

pdf("Figures for Paper/Figure_2a_percent_disagreement_partisan_and_income_by_issue_twoway.pdf",height=6,width=6,onefile=FALSE)
ggplot(order.temp, aes(y=income*100,x=partisan*100)) +
  geom_jitter(aes(colour=category, shape = category), alpha = 0.75 ,size = 4, width = 0.02, height = 0.02) +
  scale_fill_discrete(guide = guide_legend(title = "Issue Type",reverse=FALSE))+
  ylab("Mean Difference in Opinion between Income Groups")+xlab("\nMean Difference in Opinion between Parties")+
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.25))+
  ggtheme_personalized + theme(legend.position="top") +
  theme(axis.text.x=element_text(size=12)) +
  theme(axis.text.y=element_text(size=12)) +
  theme(legend.title=element_blank()) +
  theme(panel.grid.major.y=element_blank()) +
  ylim(c(-005,105))+ xlim(c(-005,105))+
  geom_segment(x = 0, xend = 100, y = 0, yend = 100, color = "black",linetype =3) +
  theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


###########################################################################
###############                   Figure 4                 ################
###########################################################################


ggdata <- ddply(df , .(issue) , function(x) out = data.frame(
	party = as.numeric(mean(x$vote[x$party=="R"]) > mean(x$vote[x$party=="D"])),
	RepMargin = mean(x$vote[x$party=="R"]) - mean(x$vote[x$party=="D"]),
	high = mean(x$Q5),
	low = mean(x$Q1)
))
ggdata$party <- ifelse(ggdata$party==1,"R bills","D bills")

pdf("Figures for Paper/Figure_4a_popularity_by_bill.pdf",height=5.5,width=5.5,onefile=FALSE)
ggplot(ggdata , aes(y=high, x=low, group = party)) +
	geom_segment(aes(x = 0, y = 0 , xend = 1 , yend = 1), colour = "black", linetype=4,size = .5) +
	geom_point(aes(colour = factor(party), shape = factor(party)), alpha = 0.75, size = 5) +
	ggtheme_personalized +
	theme(panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.position = c(0.9, 0.2),
		axis.text.x=element_text(size=14,color="black",hjust = 0.5, vjust = 0.25),
		axis.text.y=element_text(size=14,color="black"),
		axis.title.x=element_text(size=16,color="black", vjust=0),
		axis.title.y=element_text(size=16,color="black", vjust=1.25)) +
	scale_x_continuous(name="\nSupport by Low-Income Constituents\n", limits=c(-.02, 1.02), labels = c("0%" , "50%" , "100%"), breaks = c(0,0.5,1)) +
	scale_y_continuous(name="\nSupport by High-Income Constituents\n", limits=c(-.02, 1.02), labels = c("0%" , "50%" , "100%"),breaks = c(0,0.5,1)) +
	guides(size="none") +
	scale_colour_manual(values = c("#000069","#b10f2f")) +
  scale_shape_manual(values = c(19,2)) +
  annotate("segment" , x = median(subset(ggdata , party=="D bills")$low) , xend = median(subset(ggdata , party=="D bills")$low),
		y = -0.02 , yend = 0.02 , colour = "#000069")  +
	annotate("text" , x = median(subset(ggdata , party=="D bills")$low) ,
		y = 0.02 , colour = "#000069", label = "DEM MEDIAN BILL",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , x = median(subset(ggdata , party=="R bills")$low) , xend = median(subset(ggdata , party=="R bills")$low),
		y = -0.02 , yend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , x = median(subset(ggdata , party=="R bills")$low) ,
		y = 0.02 , colour = "#b10f2f", label = "REP MEDIAN BILL",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = median(subset(ggdata , party=="D bills")$high) , yend = median(subset(ggdata , party=="D bills")$high),
		x = -0.02 , xend = 0.02 , colour = "#000069")  +
	annotate("text" , y = median(subset(ggdata , party=="D bills")$high) ,
		x = 0.02 , colour = "#000069", label = "DEM MEDIAN BILL",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = median(subset(ggdata , party=="R bills")$high) , yend = median(subset(ggdata , party=="R bills")$high),
		x = -0.02 , xend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , y = median(subset(ggdata , party=="R bills")$high) ,
		x = 0.02 , colour = "#b10f2f", label = "REP MEDIAN BILL",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = 0 , yend = 1,
		x = 0, xend = 0 , colour = "black", size = .5)  +
	annotate("segment" , y = 0 , yend = 0,
		x = 0, xend = 1 , colour = "black", size = .5)  +
	theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()


ggdata <- ddply(df , .(issue) , function(x) out = data.frame(
	party = as.numeric(mean(x$vote[x$party=="R"]) > mean(x$vote[x$party=="D"])),
	RepMargin = mean(x$vote[x$party=="R"]) - mean(x$vote[x$party=="D"]),
	high = mean(x$Rep),
	low = mean(x$Dem)
))
ggdata$party <- ifelse(ggdata$party==1,"R bills","D bills")

pdf("Figures for Paper/Figure_4b_popularity_by_bill_among_partisans.pdf",height=5.5,width=5.5,onefile=FALSE)
ggplot(ggdata , aes(y=high, x=low, group = party)) +
	geom_segment(aes(x = 0, y = 0 , xend = 1 , yend = 1), colour = "black", linetype=4,size = .5) +
	geom_point(aes(colour = factor(party), shape = factor(party)), alpha = 0.75, size = 5) +
	scale_x_continuous(name="\nSupport by Democratic Constituents\n", limits=c(-.02, 1.02), labels = c("0%" , "50%" , "100%"),breaks = c(0,0.5,1)) +
	scale_y_continuous(name="\nSupport by Republican Constituents\n", limits=c(-.02, 1.02), labels = c("0%" , "50%" , "100%"),breaks = c(0,0.5,1)) +
	ggtheme_personalized +
	theme(panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.position = c(0.95, 0.2),
		axis.text.x=element_text(size=14,color="black",hjust = 0.5, vjust = 0.25),
		axis.text.y=element_text(size=14,color="black"),
		axis.title.x=element_text(size=16,color="black", vjust=0),
		axis.title.y=element_text(size=16,color="black", vjust=1.25)) +
	guides(size="none") +
	scale_colour_manual(values = c("#000069","#b10f2f")) +
	annotate("segment" , x = median(subset(ggdata , party=="D bills")$low) , xend = median(subset(ggdata , party=="D bills")$low),
		y = -0.02 , yend = 0.02 , colour = "#000069")  +
	annotate("text" , x = median(subset(ggdata , party=="D bills")$low) ,
		y = 0.02 , colour = "#000069", label = "DEM MEDIAN BILL",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , x = median(subset(ggdata , party=="R bills")$low) , xend = median(subset(ggdata , party=="R bills")$low),
		y = -0.02 , yend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , x = median(subset(ggdata , party=="R bills")$low) ,
		y = 0.02 , colour = "#b10f2f", label = "REP MEDIAN BILL",angle = 90 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = median(subset(ggdata , party=="D bills")$high) , yend = median(subset(ggdata , party=="D bills")$high),
		x = -0.02 , xend = 0.02 , colour = "#000069")  +
	annotate("text" , y = median(subset(ggdata , party=="D bills")$high) ,
		x = 0.02 , colour = "#000069", label = "DEM MEDIAN BILL",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = median(subset(ggdata , party=="R bills")$high) , yend = median(subset(ggdata , party=="R bills")$high),
		x = -0.02 , xend = 0.02 , colour = "#b10f2f")  +
	annotate("text" , y = median(subset(ggdata , party=="R bills")$high) ,
		x = 0.02 , colour = "#b10f2f", label = "REP MEDIAN BILL",angle = 0 , size = 5,hjust = 0,vjust = 0.3, fontface =2)  +
	annotate("segment" , y = 0 , yend = 1,
		x = 0, xend = 0 , colour = "black", size = .5)  +
	annotate("segment" , y = 0 , yend = 0,
		x = 0, xend = 1 , colour = "black", size = .5)  +
	theme(legend.key = element_rect(colour = "white",fill = "white"))
dev.off()

###########################################################################
###############                   Figure 3                 ################
###########################################################################

ggdata <- ddply(df , .(issue) , function(x) out = data.frame(
	RepBill = as.numeric(mean(x$vote[x$party=="R"]) > mean(x$vote[x$party=="D"])),
	RepMargin = mean(x$vote[x$party=="R"]) - mean(x$vote[x$party=="D"])
))

ggdata <- join(df , ggdata)
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata$Outpartisan <- NA
ggdata$Outpartisan[ggdata$party=="D"] <- ggdata$Rep[ggdata$party=="D"]
ggdata$Outpartisan[ggdata$party=="R"] <- ggdata$Dem[ggdata$party=="R"]

ggdata2 <- melt(ggdata, id.vars = c("senator","issue","vote","party","RepBill"), measure.vars = c("Q1","Q5","SW","Partisan","Outpartisan"))
ggdata2$color <- ggdata2$party
ggdata2$party[ggdata2$party=="D"] <- "Democratic Senators"
ggdata2$party[ggdata2$party=="R"] <- "Republican Senators"

levels(ggdata2$variable) <- c("Poor Opinion","Rich Opinion","State Opinion","Copartisan Opinion","Out-partisan Opinion")

ggdata3 <- ggdata2
ggdata3$party <- "All Senators"
ggdata2 <- rbind.data.frame(ggdata2 , ggdata3)

grid <- with(ggdata2, seq(min(value), max(value), length = 100))
normaldens <- ddply(subset(ggdata2 , RepBill==0), .(party,variable), function(df) {
  data.frame(
    predicted = density(df$value)$x,
    density = density(df$value)$y/sum(density(df$value)$y)
  )
})
normaldens$density[normaldens$predicted < 0] <- 0
normaldens$density[normaldens$predicted > 1] <- 0
normaldens$predicted[normaldens$predicted < 0] <- 0
normaldens$predicted[normaldens$predicted > 1] <- 0

normaldens1 <- ddply(subset(ggdata2 , RepBill==1), .(party,variable), function(df) {
  data.frame(
    predicted = density(df$value)$x,
    density = density(df$value)$y/sum(density(df$value)$y)
  )
})
normaldens1$density[normaldens1$predicted < 0] <- 0
normaldens1$density[normaldens1$predicted > 1] <- 0
normaldens1$predicted[normaldens1$predicted < 0] <- 0
normaldens1$predicted[normaldens1$predicted > 1] <- 0

g1 <- ggplot(data = ggdata2,aes(x = value)) +
		geom_area(data = normaldens , aes(x = predicted, y = density*50), fill = alpha(c("#000069"), .1)) +
		geom_area(data = normaldens1 , aes(x = predicted, y = density*50), colour = alpha(c("#b10f2f"), .3), fill = NA) +
		theme(axis.title.x=element_blank(),
			axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.25))+
		ggtheme_personalized +
		theme(legend.position="none") +
		theme(axis.text.x=element_text(size=8)) +
		theme(axis.text.y=element_text(size=8)) +
		theme(legend.title=element_blank()) +
		guides(size = "none", alpha="none" , density="none") +
		scale_colour_manual(values = c("#000069","#b10f2f")) +
		theme(legend.text = element_text(size=10,color="black")) +
		scale_x_continuous(name="\nOpinion", limits=c(0, 1), labels = c("0",".5","1"),breaks = c(0,0.5,1)) +
		scale_y_continuous(name="Vote\n", limits=c(0, 1), labels = c("0",".5","1"),breaks = c(0,0.5,1)) +
		geom_segment(x = 0, xend = 1, y = 0, yend = 0, color = "black")+
		geom_segment(x = 0, xend = 0, y = 0, yend = 1, color = "black")+
		geom_segment(x = 0, xend = 1, y = 0.5, yend = 0.5, color = "black")+
		geom_segment(x = 0.5, xend = 0.5, y = 0, yend = 1, color = "black")+
		geom_segment(x = 1, xend = 1, y = 0, yend = 1, color = "black")+
		geom_segment(x = 0, xend = 1, y = 1, yend = 1, color = "black")+
		theme(panel.grid.major.y=element_blank()) +
		geom_smooth(aes(y=vote), method = "lm", se = FALSE, fullrange=FALSE, span = 1, colour = "grey30") +
		geom_smooth(data = subset(ggdata2 , RepBill==1) , aes(y=vote), method = "lm", se = FALSE, fullrange=FALSE, span = 1, colour = alpha(c("#b10f2f"), .8), lwd = 0.5, linetype = "dashed") +
		geom_smooth(data = subset(ggdata2 , RepBill==0) , aes(y=vote), method = "lm", se = FALSE, fullrange=FALSE, span = 1, colour = alpha(c("#000069"), .8), lwd = 0.5, linetype = "dotted") +
		facet_grid(party~variable) +
		theme(strip.text.y = element_text(size = 11, face = "bold", colour = "black"),
			strip.text.x = element_text(size = 11, face = "bold", colour = "black"),
			strip.background = element_rect(colour = "white", fill = "white"))

png("Figures for Paper/Figure_3_responsiveness_secret_weapon.png",res=300,height=7,width=10.5,units="in")
g1
dev.off()



###########################################################################
###############                   Figure 7                 ################
###########################################################################

ggdata <- ddply(df , .(issue) , function(x) out = data.frame(
	RepBill = as.numeric(mean(x$vote[x$party=="R"]) > mean(x$vote[x$party=="D"])),
	RepMargin = mean(x$vote[x$party=="R"]) - mean(x$vote[x$party=="D"])
))

ggdata <- join(df , ggdata)
ggdata$Partisan <- NA
ggdata$Partisan[ggdata$party=="D"] <- ggdata$Dem[ggdata$party=="D"]
ggdata$Partisan[ggdata$party=="R"] <- ggdata$Rep[ggdata$party=="R"]

ggdata2 <- melt(ggdata, id.vars = c("senator","issue","vote","party","RepBill"), measure.vars = c("Q1","Q5","SW","Partisan"))
ggdata2$color <- ggdata2$party
ggdata2$party[ggdata2$party=="D"] <- "Democratic Senators"
ggdata2$party[ggdata2$party=="R"] <- "Republican Senators"

levels(ggdata2$variable) <- c("Poor Opinion","Rich Opinion","State Opinion","Copartisan Opinion","Out-partisan Opinion")

ggdata3 <- ggdata2
ggdata3$party <- "All Senators"
ggdata2 <- rbind.data.frame(ggdata2 , ggdata3)

grid <- with(ggdata2, seq(min(value), max(value), length = 100))
normaldens <- ddply(subset(ggdata2 , RepBill==0), .(party,variable), function(df) {
  data.frame(
    predicted = density(df$value)$x,
    density = density(df$value)$y/sum(density(df$value)$y)
  )
})
normaldens$density[normaldens$predicted < 0] <- 0
normaldens$density[normaldens$predicted > 1] <- 0
normaldens$predicted[normaldens$predicted < 0] <- 0
normaldens$predicted[normaldens$predicted > 1] <- 0

normaldens1 <- ddply(subset(ggdata2 , RepBill==1), .(party,variable), function(df) {
  data.frame(
    predicted = density(df$value)$x,
    density = density(df$value)$y/sum(density(df$value)$y)
  )
})
normaldens1$density[normaldens1$predicted < 0] <- 0
normaldens1$density[normaldens1$predicted > 1] <- 0
normaldens1$predicted[normaldens1$predicted < 0] <- 0
normaldens1$predicted[normaldens1$predicted > 1] <- 0

ggdata2$vote <- as.numeric(ggdata2$vote==1 & ggdata2$value>0.5 | ggdata2$vote==0 & ggdata2$value<0.5)
g1 <- ggplot(data = ggdata2,aes(x = value)) +
		geom_area(data = normaldens , aes(x = predicted, y = density*50), fill = alpha(c("#000069"), .1)) +
		geom_area(data = normaldens1 , aes(x = predicted, y = density*50), colour = alpha(c("#b10f2f"), .3), fill = NA) +
		theme(axis.title.x=element_blank(),
			axis.text.x = element_text(angle = 0, hjust = 1, vjust = 0.25))+
		ggtheme_personalized +
		theme(legend.position="none") +
		theme(axis.text.x=element_text(size=8)) +
		theme(axis.text.y=element_text(size=8)) +
		theme(legend.title=element_blank()) +
		guides(size = "none", alpha="none" , density="none") +
		scale_colour_manual(values = c("#000069","#b10f2f")) +
		theme(legend.text = element_text(size=10,color="black")) +
		scale_x_continuous(name="\nOpinion", limits=c(0, 1), labels = c("0",".5","1"), breaks = c(0,0.5,1)) +
		scale_y_continuous(name="Congruent Votes\n", limits=c(0, 1), labels = c("0",".5","1"), breaks = c(0,0.5,1)) +
		geom_segment(x = 0, xend = 1, y = 0, yend = 0, color = "black")+
		geom_segment(x = 0, xend = 0, y = 0, yend = 1, color = "black")+
		geom_segment(x = 0, xend = 1, y = 0.5, yend = 0.5, color = "black")+
		geom_segment(x = 0.5, xend = 0.5, y = 0, yend = 1, color = "black")+
		geom_segment(x = 1, xend = 1, y = 0, yend = 1, color = "black")+
		geom_segment(x = 0, xend = 1, y = 1, yend = 1, color = "black")+
		theme(panel.grid.major.y=element_blank()) +
		geom_smooth(aes(y=vote), method = "loess", se = FALSE, fullrange=FALSE, span = 1, colour = "grey30") +
		geom_smooth(data = subset(ggdata2 , RepBill==1) , aes(y=vote), method = "loess", se = FALSE, fullrange=FALSE, span = 1, colour = alpha(c("#b10f2f"), .5), lwd = 0.5, linetype = "dashed") +
		geom_smooth(data = subset(ggdata2 , RepBill==0) , aes(y=vote), method = "loess", se = FALSE, fullrange=FALSE, span = 1, colour = alpha(c("#000069"), .5), lwd = 0.5, linetype = "dotted") +
		facet_grid(party~variable) +
		theme(strip.text.y = element_text(size = 11, face = "bold", colour = "black"),
			strip.text.x = element_text(size = 11, face = "bold", colour = "black"),
			strip.background = element_rect(colour = "white", fill = "white"))

png("Figures for Paper/Figure_7_responsiveness_secret_weapon_congruence.png",res=300,height=7,width=10.5,units="in")
g1
dev.off()


###########################################################################
###############                   Figure 5                 ################
###########################################################################


ggdata <- ddply(df , .(issue) , function(x) out = data.frame(
	RepBill = as.numeric(mean(x$vote[x$party=="R"]) > mean(x$vote[x$party=="D"])),
	RepMargin = mean(x$vote[x$party=="R"]) - mean(x$vote[x$party=="D"])
))

ggdata <- join(df , ggdata)

ggdata <- ddply(ggdata , .(issue,category,RepBill) , function(x) out = data.frame(
	RepVotes = mean(x$vote[x$party=="R"]),
	DemVotes = mean(x$vote[x$party=="D"]),
	SW = mean(x$SW)
))

ggdata <- melt(ggdata , id.vars = c("issue","category","RepBill","SW"))
ggdata$RepBill <- factor(ggdata$RepBill)
levels(ggdata$RepBill) <- c("Democratic Bills","Republican Bills")
levels(ggdata$variable) <- c("R senators","D senators")

pdf("Figures for Paper/Figure_5_mean_elite_voting_vs_SWopinionv2.pdf",height=5,width=10,onefile=FALSE)
ggplot(ggdata, aes(x=SW*100 , y = value*100 , fill = paste(variable) , color = paste(variable) , shape = paste(variable))) +
	geom_segment(x = 0, xend = 100, y = 0, yend = 0, color = "black")+
	geom_segment(x = 0, xend = 0, y = 0, yend = 100, color = "black")+
	geom_point(size = 5, alpha = 0.5)+
	geom_smooth(method = "lm", se = FALSE, fullrange=FALSE, span = 1, lwd = 0.5, linetype = "dashed", show.legend=FALSE) +
	geom_smooth(method = "loess", se = FALSE, fullrange=FALSE, span = .9, lwd = 0.5, linetype = "dotted",formula = y ~ log(x),show.legend=FALSE) +
	geom_segment(x = mean(subset(ggdata , RepBill==0)$SW*100),xend = mean(subset(ggdata , RepBill=="Democratic Bills")$SW*100),y = 0, yend = 5, linetype = "solid", colour = "#000069") +
	geom_segment(x = mean(subset(ggdata , RepBill==1)$SW*100),xend = mean(subset(ggdata , RepBill=="Republican Bills")$SW*100),y = 0, yend = 5, linetype = "solid", colour = "#b10f2f") +
	scale_x_continuous(name="Support by Statewide Constituents\n", limits=c(0, 1.1*100), labels = c("0%" , "50%" , "100%"), breaks = c(0,0.5*100,1*100)) +
	scale_y_continuous(name="\nVoting Support by Senators", limits=c(0, 1.1*100), labels = c("0%" , "50%" , "100%"),breaks = c(0,0.5*100,1*100)) +
	ggtheme_personalized +
	theme(plot.margin = unit(c(0.,0.,0.,0.), "cm")) +
	theme(panel.grid.major.y=element_blank(),
		legend.title=element_blank(),
		legend.position = c(0.9, 0.5),
		axis.text.x=element_text(size=14,color="black"),
		axis.text.y=element_text(size=14,color="black"),
		axis.title.x=element_text(size=16,color="black", vjust=0),
		axis.title.y=element_text(size=16,color="black", vjust=1.25)) +
	scale_colour_manual(values = c("#000069","#b10f2f")) +
	scale_fill_manual(values = c("#000069","#b10f2f")) +
  scale_shape_manual(values = c(19,2)) +
	facet_grid(~factor(RepBill)) +
	theme(strip.text.y = element_text(size = 11, face = "bold", colour = "black"),
		strip.text.x = element_text(size = 11, face = "bold", colour = "black"),
		strip.background = element_rect(colour = "white", fill = "white"))
dev.off()


